<html>
<head>
    <meta charset="utf-8"/>
    <title>${app.appName}</title>
    <#include "/include/head-file.ftl"/>
    <link rel="stylesheet" type="text/css" href="${base}/static/plugins/TextInputEffects/css/normalize.css"/>
    <link rel="stylesheet" type="text/css" href="${base}/static/plugins/TextInputEffects/css/set1.css"/>
    <script src="${base}/static/gdca/js/base64.js" type="text/javascript" charset="utf-8"></script>
    <script src="${base}/static/gdca/js/mToken.js" type="text/javascript" charset="utf-8"></script>
    <script src="${base}/static/gdca/js/mTokenBasicOper.js" type="text/javascript" charset="utf-8"></script>
    <style>
        [v-cloak] {
            display: none;
        }
    </style>
</head>
<body class="custom-body">
<div id="app" v-cloak>
    <div class="app-main-box" @keydown.13='login'>
        <#--<div class="app-bg-box"></div>-->
        <div class="app-center-box">
            <div class="left_box">
                <img src="${base}/static/images/logo_title.png"/>
                <h1>${app.appName}后台管理系统</h1>
                <p>智库数据，智能设备数据收集，平台数据导出，深度洞察用户需求，自定义用户数据，帮企业数据驱动，数据监控，可多维度数据分析，精细化数据，秒推送，快速部署，数据级，实时更新。</p>
            </div>
            <div class="right_box">
                    <div class="box_form_item_title">用户登录</div>
                    <el-form :model="form" ref="form" :rules="rules">
                        <template v-if="isCertShow">
                            <div class="box_form_item">
                                <el-form-item prop="certificateUser" class="beforeRequiredSign">
                                    <el-select v-model="form.certificateUser"
                                               :style="{width: '100%'}"
                                               :filterable="false"
                                               :disabled="false"
                                               :multiple="false" :clearable="true"
                                               placeholder="请选择证书">
                                        <el-option v-for='item in cerListArr' :key="item.value" :value="item.value"
                                                   :label="item.title"></el-option>
                                    </el-select>
                                </el-form-item>
                            </div>
                            <div class="box_form_item">
                                <el-form-item prop="certificatePwd" class="beforeRequiredSign">
    <#--                                <i class="item-icon item-icon-lock"></i>-->
                                    <input v-model="form.certificatePwd" type="password" id="user_pin" name="user_pin" placeholder="请输入证书密码" />
                                </el-form-item>
                            </div>
                        </template>
                        <div class="box_form_item box_form_item_code">
                            <el-form-item prop="managerName" class="beforeRequiredSign">
                                 <i class="item-icon item-icon-user"></i>
                                <input v-model="form.managerName" type="text" id="input-name" placeholder="用户名" />
                                  <#--<label class="input__label input__label--hoshi input__label--hoshi-color-1" for="input-name">-->
                                      <#--<span class="input__label-content input__label-content--hoshi">账号</span>-->
                                  <#--</label>-->
                            </el-form-item>
                        </div>
                        <div class="box_form_item box_form_item_code">
                            <el-form-item prop="managerPassword" class="beforeRequiredSign">
                                <i class="item-icon item-icon-lock"></i>
                                <input v-model="form.managerPassword" type="password" id="input-password" placeholder="密码" />
                                  <#--<span class="input input--hoshi">
                                          <input v-model="form.managerPassword" class="input__field input__field--hoshi" type="password"
                                                 id="input-password"/>
                                          <label class="input__label input__label--hoshi input__label--hoshi-color-1"
                                                 for="input-password">
                                              <span class="input__label-content input__label-content--hoshi">密码</span>
                                          </label>
                                      </span>-->
                            </el-form-item>
                        </div>
                        <div class="box_form_item_code">
                            <div class="code_1">
                                <el-form-item prop="rand_code" class="beforeRequiredSign">
                                    <i class="item-icon item-icon-code"></i>
                                    <input v-model="form.rand_code" type="text" id="input-rand-code" placeholder="验证码" />
                                     <#--<span class="input input--hoshi">
                                          <input v-model="form.rand_code" class="input__field input__field--hoshi" type="text"
                                                 id="input-rand-code"/>
                                          <label class="input__label input__label--hoshi input__label--hoshi-color-1"
                                                 for="input-rand-code">
                                              <span class="input__label-content input__label-content--hoshi">验证码</span>
                                          </label>
                                      </span>-->
                                </el-form-item>
                            </div>
                            <div class="code_2">
                                <img :src="verifCode" class="code-img" @click="code" title="看不清？点击换一张"/>
                            </div>
                            <#--<div class="class-21">
                                <div @click="code" class="class-22">
                                    <div class="class-23">看不清？</div>
                                    <div class="class-24">换一张</div>
                                </div>
                            </div>-->
                        </div>
                    </el-form>
                    <#--<div class="class-25"></div>-->
                    <el-button @click="handleLogin" type="danger" :loading="loading" class="login-button">
                        {{loading?'登录中':'登录'}}
                    </el-button>
                    <div style="margin-top: 20px;" v-if="isCertShow">
                        <el-link type="success" icon="el-icon-download"></el-link>
                        <el-link type="primary" :underline="false" @click="onCertDownLoap">USBKEY控件下载</el-link>
                    </div>
            </div>
        </div>
    </div>
</div>
</body>
</html>
<script src="${base}/static/plugins/TextInputEffects/js/classie.js"></script>
<script>
    var app = new Vue({
        el: '#app',
        watch: {},
        data: {
            base: ms.base,
            loading: false,
            form: {
                certificateUser: [],
                certificatePwd: '',
                managerName: '',
                managerPassword: '',
                rand_code: '',
                rememberMe: true
            },
            rules: {
                certificateUser: [
                    {required: true, message: '请选择证书', trigger: 'blur'},
                ],
                certificatePwd: [
                    {required: true, message: '请输入证书密码', trigger: 'blur'}
                ],
                managerName: [
                    {required: true, message: '请输入账号', trigger: 'blur'},
                    {min: 1, max: 30, message: '长度不能超过30个字符', trigger: 'change'}
                ],
                managerPassword: [
                    {required: true, message: '请输入密码', trigger: 'blur'},
                    {min: 1, max: 30, message: '长度不能超过30个字符', trigger: 'change'}
                ],
                rand_code: [
                    {required: true, message: '请输入验证码', trigger: 'blur'},
                    {min: 1, max: 4, message: '长度不能超过4个字符', trigger: 'change'}
                ],
            },
            verifCode: ms.manager + "/code.do?t=" + new Date().getTime(),
            cerListArr: [],
            isCertShow: true,
            userSignCertB64: "",
            loginTag: 0 //默认0是签名登录，1是普通登录
        },
        methods: {
            //登录
            handleLogin: function () {
                var that = this;
                that.$refs.form.validate(function (valid) {
                    if (valid) {
                        if (that.isCertShow) {
                            //验证pin口令，即证书密码
                            var ret = verifyUserPin();  //证书密码输入框写上 id="user_pin" name="user_pin"
                            // console.log("ret")
                            // console.log(ret)
                            if (ret != 0) {
                                return;
                            }
                            //导出证书
                            console.log(that.form.certificateUser)
                            // exportUserCert(this.form.certificateUser, "1");
                            that.userSignCertB64 = token.SOF_ExportUserCert(that.form.certificateUser, "1");
                            // console.log("userSignCertB64")
                            // console.log(that.userSignCertB64)
                            that.validateCertGenRandom() //根据证书获取随机数
                        }else {
                            that.login() //普通登录
                        }

                    }
                });
            },
            login:function (params){
                // console.log("params")
                // console.log(params)
                this.loading = true;
                let loginParams = {
                    managerName: this.form.managerName,
                    managerPassword: this.form.managerPassword,
                    rand_code: this.form.rand_code,
                    rememberMe: this.form.rememberMe,
                    loginTag: this.loginTag
                }
                //加上证书验证参数
                if (params) {
                    loginParams = Object.assign(loginParams, params)
                }
                ms.http.post(ms.manager + "/login.do", loginParams).then(res => {
                    if (res.result) {
                        location.href = ms.manager + "/index.do";
                    } else {
                        this.$notify({
                            title: '失败',
                            message: res.msg,
                            type: 'error'
                        });
                        this.code();
                        this.loading = false;
                    }
                }).finally(function () {
                    this.loading = false;
                });
            },
            //获取验证码
            code: function () {
                this.verifCode = ms.web + "/code.do?t=" + new Date().getTime();
            },
            //初始
            initial: function () {
                this.form.managerName = localStorage.getItem('managerName');
                this.form.managerPassword = localStorage.getItem('managerPassword');
                top.location != self.location ? (top.location = self.location) : '';
            },
            //证书验证，获取随机数
            validateCertGenRandom: function () {
                let that = this
                // ms.http.post(ms.manager + "/login.do", loginParams).then(function (res) {
                ms.http.get("/usb/code", {userSignCertB64: this.userSignCertB64 }).then(function (res) {
                    if (res.result) {
                        let sRandomDataB64 = res.data //获取随机数
                        console.log("随机数：")
                        console.log(sRandomDataB64)
                        let signed = signData(that.form.certificateUser, sRandomDataB64); //对随机数进行签名,加密
                        console.log("随机数加密：")
                        console.log(signed)
                        if (signed != null && signed != "") {
                            let params = {
                                userSignCert: that.userSignCertB64,
                                randomData: sRandomDataB64,
                                signedDataB64: signed,
                            }
                            that.login(params) //证书登录
                        }
                    } else {
                        that.$emit('validateFail')
                        that.$message.error(res.message)
                    }
                }).catch(() => {
                    this.$emit('validateFail')
                    this.$message.error(res.message)
                })
            },
            getQueryString: function (name) {
                let reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i')
                let r = window.location.search.substr(1).match(reg)
                if (r != null) {
                    return encodeURIComponent(r[2])
                }
                return null
            },
            onCertDownLoap() {
                let dom = document.createElement("a");
                dom.href = "/static/file/GM3000_mPlugin_Setup(202111打包证书客户端).exe";
                dom.download = "GM3000_mPlugin_Setup(202111打包证书客户端).exe";
                document.body.appendChild(dom);
                // 点击下载
                dom.click();
                document.body.removeChild(dom);
            },
        },
        created: function () {
            // 默认情况有证书登录，参数type为1则没有证书登录
            let type = this.getQueryString('type')
            console.log("type：" + type)
            if (type == '1') {
                this.isCertShow = false
                this.loginTag = 1
            } else {
                this.isCertShow = true
                this.loginTag = 0
            }
            this.code();
            this.initial();
        },
        mounted() {
            //识别并获取证书列表数据
            if (this.isCertShow) {
                enumDevice();
                let cerList = token.SOF_GetUserList();
                console.log("证书数量：" + cerList.length)
                if (cerList != null && cerList.length > 0) {
                    for (var i = 0; i < cerList.length; i++) {
                        this.cerListArr.push({ title: cerList[i][0], value: cerList[i][1] })
                    }
                }
            }
        },
    })
</script>
<#--<script>
    (function () {
        // trim polyfill : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/Trim
        if (!String.prototype.trim) {
            (function () {
                // Make sure we trim BOM and NBSP
                var rtrim = /^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g;
                String.prototype.trim = function () {
                    return this.replace(rtrim, '');
                };
            })();
        }

        [].slice.call(document.querySelectorAll('input.input__field')).forEach(function (inputEl) {
            // in case the input is already filled..
            if (inputEl.value.trim() !== '') {
                classie.add(inputEl.parentNode, 'input--filled');
            }

            // events:
            inputEl.addEventListener('focus', onInputFocus);
            inputEl.addEventListener('blur', onInputBlur);
        });

        function onInputFocus(ev) {
            classie.add(ev.target.parentNode, 'input--filled');
        }

        function onInputBlur(ev) {
            if (ev.target.value.trim() === '') {
                classie.remove(ev.target.parentNode, 'input--filled');
            }
        }
    })();
</script>-->

<style>
    .beforeRequiredSign::before {
        position: absolute;
        left: -15px;
        top: 14px;
        margin-right: 4px;
        color: #f5222d;
        font-size: 14px;
        font-family: SimSun, sans-serif;
        line-height: 1;
        content: '*';
    }
    .el-form-item {
        margin-bottom: 0px;
        position: relative;
    }

    .el-form-item__content {
        line-height: initial;
    }

    .custom-body {
    }

    .app-main-box {
        outline: none;
        outline-offset: -1px;
        height: 100%;
        max-width: 100%;
        align-items: flex-start;
        flex-direction: row;
        display: flex;
        justify-content: center;
        animation-duration: 1s;
        width: 100%;
        position: relative;
        background-color: #f2f7ff;
        background-repeat: no-repeat;
        background-image: url(${base}/static/images/login-bg.jpg);
        background-size: cover;
        background-position: center;
    }

    .app-bg-box {
        position: absolute;
        right: 0;
        top: 0;
        bottom: 0;
        width: 466px;
        /*height: 100%;*/
        z-index: 0;
        background-repeat: no-repeat;
        background-color: #f2f7ff;
        background-image: url(${base}/static/images/login-bg-right.jpg);
        background-size: cover;
        background-position:right;
    }

    .app-center-box {
        /*box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);*/
        /*color: #333333;*/
        outline: none;
        outline-offset: -1px;
        height: auto;
        padding-top: 13vh;
        max-width: 100%;
        /*background-color: rgba(255, 255, 255, 1);*/
        flex-direction: row;
        display: flex;
        justify-content: space-between;
        animation-duration: 1s;
        /*border-radius: 12px;*/
        width: 960px;
        /*z-index: 1;
        background-repeat: no-repeat;
        background-image: url(${base}/static/images/login-bg-i.png);
        background-size: inherit;*/
    }

    .left_box {
        color: #333333;
        outline: none;
        outline-offset: -1px;
        /*height: 100%;*/
        /*max-width: 100%;*/
        /*align-items: flex-start;*/
        /*flex-direction: row;*/
        /*display: flex;*/
        /*justify-content: flex-start;*/
        /*animation-duration: 1s;*/
        /*flex: 0 0 360px;*/
        /*flex: 1;*/
        width: 360px;
        position: relative;
        /*background-repeat: no-repeat;*/
    }
    .left_box:before {
        content: '';
        display: block;
        position: absolute;
        left: -93px;
        top: 5px;
        width: 75px;
        height: 68px;
        /*background-color: #00a0e9;*/
        background-repeat: no-repeat;
        background-image: url(${base}/static/images/login-bg-icon1.png);
        background-size: 100% 100%;
    }
    .left_box h1 {
        font-weight: normal;
        margin-top: 1em;
        font-size: 36px;
    }
    .left_box p{
        /*width: 58%;*/
        font-size: 16px;
        color: #7c818a;
        line-height: 1.8em;
    }

    .right_box {
        color: #333333;
        outline: none;
        outline-offset: -1px;
        /*flex: 0 0 200px;*/
        /*flex: 1;*/
        padding: 50px 70px 60px 70px;
        /*height: 100%;*/
        /*max-width: 100%;*/
        /*align-items: center;*/
        flex-direction: column;
        display: flex;
        justify-content: flex-start;
        animation-duration: 1s;
        width: 450px;
        margin-right: -78px;
        /*background-repeat: no-repeat;*/
        border-radius: 8px;
        box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
        background-color: rgba(255, 255, 255, 1);
        position: relative;
    }
    .right_box:after {
        position: absolute;
        content: '';
        display: block;
        bottom: -80px;
        left: 50%;
        margin-left: -75px;
        width: 150px;
        height: 66px;
        /*background-color: #00a0e9;*/
        background-repeat: no-repeat;
        background-image: url(${base}/static/images/login-bg-icon2.png);
        background-size: 100% 100%;
    }

    .box_form_item_title {
        color: #333333;
        word-wrap: break-word;
        display: inline-block;
        animation-duration: 1s;
        font-size: 24px;
        line-height: 1.4;
        padding-bottom: 12px;
        border-bottom: 1px #ccc solid;
    }

    .box_form_item {
        color: #333333;
        outline: none;
        outline-offset: -1px;
        /*height: 80px;*/
        max-width: 100%;
        flex-direction: column;
        display: flex;
        justify-content: flex-end;
        animation-duration: 1s;
        width: 100%;
        background-repeat: no-repeat;
        border: 1px #ccc solid;
        border-radius: 5px;
        margin-top: 24px;
    }
    .box_form_item .el-form-item__content {
        position: relative;
    }
    .box_form_item .el-input__inner{
        padding-left: 0!important;
    }
    .box_form_item_code {
        color: #333333;
        outline: none;
        outline-offset: -1px;
        /*height: 80px;*/
        max-width: 100%;
        align-items: flex-end;
        flex-direction: row;
        display: flex;
        justify-content: flex-start;
        animation-duration: 1s;
        background-repeat: no-repeat;
        border: 1px #ccc solid;
        border-radius: 5px;
        margin-top: 24px;
    }
    /*.box_form_item i,*/
    .box_form_item_code i {
        position: absolute;
        left: 12px;
        top: 50%;
        margin-top: -10px;
        font-size: 22px;
        color: #999;
    }
    .box_form_item input,
    .box_form_item_code input {
        text-indent: 3em;
        width: 100%;
        border: 0;
        line-height: 44px;
        border-radius: 5px;
    }
    .box_form_item input:focus,
    .box_form_item_code input:focus {
        outline: none;
    }
    .box_form_item input::-webkit-input-placeholder,
    .box_form_item_code input::-webkit-input-placeholder {
        font-size: 16px;
    }

    .box_form_item .item-icon,
    .box_form_item_code .item-icon{
        display: inline-block;
        width: 21px;
        height: 23px;
    }
    .item-icon.item-icon-user{
        background-repeat: no-repeat;
        background-image: url(${base}/static/images/user_icon.png);
        background-size: cover;
        background-position: center;
    }
    .item-icon.item-icon-lock{
        background-repeat: no-repeat;
        background-image: url(${base}/static/images/lock_icon.png);
        background-size: cover;
        background-position: center;
    }
    .item-icon.item-icon-code{
        background-repeat: no-repeat;
        background-image: url(${base}/static/images/code_icon.png);
        background-size: 100% 100%;
        background-position: center;
    }

    .code_1 {
        color: #333333;
        outline: none;
        outline-offset: -1px;
        flex: 1;
        /*height: 80px;*/
        max-width: 100%;
        flex-direction: column;
        display: flex;
        justify-content: flex-end;
        animation-duration: 1s;
        width: 200px;
        background-repeat: no-repeat;
    }

    .code_2 {
        cursor: pointer;
        /*color: #333333;*/
        margin-right: 10px;
        outline-offset: -1px;
        /*height: 40px;*/
        max-width: 100%;
        align-items: center;
        flex-direction: row;
        display: flex;
        justify-content: center;
        /*margin-left: 10px;*/
        animation-duration: 1s;
        /*width: 88px;*/
        background-repeat: no-repeat;
        /*margin-bottom: 0.85em;*/
    }

    .login-button {
        background-color: #ea4f4f;
        margin-top: 20px;
        padding-bottom: 16px;
        padding-top: 16px;
        font-size: 16px;
    }
</style>
